package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * httpclient     底层
     * resttemplate   高度封装
     */

    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")  //指定降级方法的方法名
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        log.info("调用后台商品服务，获取商品列表");
        JsonResult r = restTemplate.getForObject("http://item-service/{1}", JsonResult.class, orderId);
        return r;
    }

    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    public JsonResult decreaseNumber(@RequestBody List<Item> items){
        log.info("调用后台商品服务，减少商品库存");
        JsonResult r = restTemplate.postForObject(
                "http://item-service/decreaseNumber",
                items,
                JsonResult.class);
        return r;
    }

    @GetMapping("/user-service/{userId}")
    @HystrixCommand(fallbackMethod = "getUserFB")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject(
                "http://user-service/{1}",
                JsonResult.class,
                userId);
    }

    @GetMapping("/user-service/{userId}/{score}")
    @HystrixCommand(fallbackMethod = "addScoreFB")
    public JsonResult addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject(
                "http://user-service/{1}/score?socre={2}",
                JsonResult.class,
                userId,
                score);
    }

    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject(
                "http://order-service/{1}",
                JsonResult.class,
                orderId);
    }

    @GetMapping("/order-service")
    @HystrixCommand(fallbackMethod = "addOrderFB")
    public JsonResult addOrder(){
        return restTemplate.getForObject(
                "http://order-service/",
                JsonResult.class);
    }

    //降级方法的参数和返回值，需要和原始方法一致，方法名任意
    public JsonResult<List<Item>> getItemsFB(String orderId){
        return JsonResult.err("获取订单列表商品失败");
    }
    public JsonResult decreaseNumberFB(List<Item> items){
        return JsonResult.err("减少商品库存失败");
    }
    public JsonResult<User> getUserFB(Integer userId){
        return JsonResult.err("获取用户信息失败");
    }
    public JsonResult addScoreFB(Integer userId,Integer score){
        return JsonResult.err("用户增加积分失败");
    }
    public JsonResult<Order> getOrderFB(String orderId){
        return JsonResult.err("获取订单失败");
    }
    public JsonResult addOrderFB(){
        return JsonResult.err("增加订单失败");
    }

}
